package org.ptkparty.client.view.players;

import org.ptkparty.client.activity.PlayersActivity;
import org.ptkparty.client.model.UserProxy;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.IsEditor;
import com.google.gwt.editor.client.adapters.EditorSource;
import com.google.gwt.editor.client.adapters.ListEditor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class PlayersView extends Composite implements IsEditor<ListEditor<UserProxy, PlayerView>> {

	private static PlayersViewUiBinder uiBinder = GWT.create(PlayersViewUiBinder.class);

	interface PlayersViewUiBinder extends UiBinder<Widget, PlayersView> {
	}

	private ListEditor<UserProxy, PlayerView> editor;
	private PlayersActivity activity;
	private SingleSelectionModel<UserProxy> selectionModel;
	private ListDataProvider<UserProxy> provider;

	@UiField(provided = true)
	CellList<UserProxy> userList;

	@UiField
	SimplePanel playerPanel;

	@UiField
	Button addBtn;

	@UiHandler("addBtn")
	public void onAddBtnClicked(ClickEvent event) {
		UserProxy newUser = activity.addUser();
		editor.getList().add(newUser);
		provider.refresh();
		selectionModel.setSelected(newUser, true);
	}

	@UiField
	Button deleteBtn;

	@UiHandler("deleteBtn")
	public void onDeleteBtnClicked(ClickEvent event) {
		activity.delete(selectionModel.getSelectedObject());
	}

	@UiField
	Button saveBtn;

	@UiHandler("saveBtn")
	public void onSaveBtnClicked(ClickEvent event) {
		activity.save();
	}

	public void removeUserEditor(UserProxy selectedProxy) {
		editor.getList().remove(selectedProxy);
		provider.getList().remove(selectedProxy);
		provider.refresh();
	}

	public PlayersView() {
		userList = new CellList<UserProxy>(new PlayerCell());

		provider = new ListDataProvider<UserProxy>();
		provider.addDataDisplay(userList);

		editor = ListEditor.of(new EditorSource<PlayerView>() {

			@Override
			public PlayerView create(int index) {
				return new PlayerView();
			}

		});

		selectionModel = new SingleSelectionModel<UserProxy>();
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				playerPanel.clear();
				playerPanel.add(editor.getEditors().get(provider.getList().indexOf(selectionModel.getSelectedObject())));
			}
		});
		userList.setSelectionModel(selectionModel);

		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setPresenter(PlayersActivity playersActivity) {
		this.activity = playersActivity;
	}

	@Override
	public ListEditor<UserProxy, PlayerView> asEditor() {
		return editor;
	}

	public void initProvider() {
		provider.setList(editor.getList());
	}

	public void selectFirstUser() {
		selectionModel.setSelected(provider.getList().get(0), true);
	}

	public void refreshList() {
		provider.refresh();
	}

}
